/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserProfile.java
 *
 * Created on May 17, 2011, 2:59:08 PM
 */

package c0907m.group2.aitsm.gui;

import c0907m.group2.aitsm.core.db.UserDb;
import c0907m.group2.aitsm.core.entity.UserInfo;
import c0907m.group2.aitsm.gui.component.StatusBar;
import c0907m.group2.aitsm.util.Helper;
import c0907m.group2.aitsm.util.Settings;

import javax.swing.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author ISTC
 */
public class UserProfile extends javax.swing.JPanel {
    private UserInfo loginUser;
    private StatusBar statusBar;
    public static final String DEFAULT_STATUS = "Manage your profile information.";
    public final Logger logger = Logger.getLogger(UserProfile.class.getName());


    /**
     * Creates new form UserProfile
     */
    public UserProfile() {
        initComponents();
        pnChangeProfile.setVisible(false);
        pnChangePw.setVisible(false);
    }

    public void setStatusBar(StatusBar sb) {
        statusBar = sb;
    }

    public void setLoginUser(UserInfo u) {
        loginUser = u;
        txtName.setText(loginUser.getName());
        txtMobile.setText(loginUser.getMobile());
    }

    /**
     * Validate & update new user's password to database
     */
    private void doSavePassword() {
        try {
            String oldPw = String.copyValueOf(jpfOldPw.getPassword());
            String newPw = String.copyValueOf(jpfNewPw.getPassword());
            String confirmPw = String.copyValueOf(jpfConfirmPw.getPassword());

            if (!Helper.MD5(oldPw).equals(loginUser.getPassword())) {
                JOptionPane.showMessageDialog(
                        this,
                        "Your old password is invalid.",
                        "Error",
                        JOptionPane.ERROR_MESSAGE
                );
            } else if (!newPw.equals(confirmPw)) {
                JOptionPane.showMessageDialog(
                        this,
                        "Password do not match.",
                        "Message",
                        JOptionPane.INFORMATION_MESSAGE
                );
            } else {
                Helper.toggle(pnChangePw);
                loginUser.setPassword(Helper.MD5(newPw));
                new UserDb().update(loginUser);
                statusBar.setMessage("Your password has been changed.", StatusBar.MESSAGE_TYPE);
            }
        } catch (SQLException e) {
            logger.log(Level.WARNING, null, e);
            statusBar.setMessage("Error connecting to database", StatusBar.ERROR_TYPE);
        }
    }

    /**
     * Update new information to database
     */
    private void doSaveInfo() {
        loginUser.setName(txtName.getText().trim());
        loginUser.setMobile(txtMobile.getText().trim());
        try {
            Helper.toggle(pnChangeProfile);
            jpfOldPw.setText("");
            jpfNewPw.setText("");
            jpfConfirmPw.setText("");
            new UserDb().update(loginUser);
            statusBar.setMessage("Your profile information has been saved.", StatusBar.MESSAGE_TYPE);
        } catch (SQLException ex) {
            logger.log(Level.WARNING, null, ex);
            statusBar.setMessage("Error connecting to database", StatusBar.ERROR_TYPE);
        }
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnToggleInfo = new javax.swing.JButton();
        pnChangeProfile = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtMobile = new javax.swing.JTextField();
        btnSaveInfo = new javax.swing.JButton();
        btnTogglePw = new javax.swing.JButton();
        pnChangePw = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jpfOldPw = new javax.swing.JPasswordField();
        jpfNewPw = new javax.swing.JPasswordField();
        jpfConfirmPw = new javax.swing.JPasswordField();
        btnSavePw = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("USER INFORMATION");

        btnToggleInfo.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        btnToggleInfo.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/dropDown_icon.png"));
        btnToggleInfo.setText("Change profile information");
        btnToggleInfo.setContentAreaFilled(false);
        btnToggleInfo.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnToggleInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToggleInfoActionPerformed(evt);
            }
        });

        pnChangeProfile.setBackground(new java.awt.Color(255, 255, 255));

        jLabel2.setText("Name:");

        jLabel3.setText("Mobile:");

        btnSaveInfo.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/save_btn.gif"));
        btnSaveInfo.setText("Save");
        btnSaveInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveInfoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnChangeProfileLayout = new javax.swing.GroupLayout(pnChangeProfile);
        pnChangeProfile.setLayout(pnChangeProfileLayout);
        pnChangeProfileLayout.setHorizontalGroup(
                pnChangeProfileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnChangeProfileLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(pnChangeProfileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(btnSaveInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(pnChangeProfileLayout.createSequentialGroup()
                                                .addGroup(pnChangeProfileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(pnChangeProfileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(txtMobile, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                                                        .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE))))
                                .addContainerGap())
        );
        pnChangeProfileLayout.setVerticalGroup(
                pnChangeProfileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnChangeProfileLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(pnChangeProfileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(27, 27, 27)
                                .addGroup(pnChangeProfileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel3)
                                        .addComponent(txtMobile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(btnSaveInfo)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnTogglePw.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        btnTogglePw.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/dropDown_icon.png"));
        btnTogglePw.setText("Change login password");
        btnTogglePw.setContentAreaFilled(false);
        btnTogglePw.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnTogglePw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTogglePwActionPerformed(evt);
            }
        });

        pnChangePw.setBackground(new java.awt.Color(255, 255, 255));

        jLabel4.setText("Old password:");

        jLabel5.setText("New password:");

        jLabel6.setText("Confirm password:");

        btnSavePw.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/save_btn.gif"));
        btnSavePw.setText("Save");
        btnSavePw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSavePwActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnChangePwLayout = new javax.swing.GroupLayout(pnChangePw);
        pnChangePw.setLayout(pnChangePwLayout);
        pnChangePwLayout.setHorizontalGroup(
                pnChangePwLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnChangePwLayout.createSequentialGroup()
                                .addGroup(pnChangePwLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(pnChangePwLayout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(btnSavePw, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnChangePwLayout.createSequentialGroup()
                                                .addContainerGap()
                                                .addGroup(pnChangePwLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(pnChangePwLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(jpfConfirmPw)
                                                        .addComponent(jpfNewPw)
                                                        .addComponent(jpfOldPw, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))))
                                .addContainerGap(97, Short.MAX_VALUE))
        );
        pnChangePwLayout.setVerticalGroup(
                pnChangePwLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnChangePwLayout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addGroup(pnChangePwLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel4)
                                        .addComponent(jpfOldPw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(pnChangePwLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel5)
                                        .addComponent(jpfNewPw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(pnChangePwLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel6)
                                        .addComponent(jpfConfirmPw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(btnSavePw)
                                .addContainerGap(49, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 922, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(310, 310, 310)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(pnChangeProfile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(pnChangePw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnToggleInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnTogglePw, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(251, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel1)
                                .addGap(33, 33, 33)
                                .addComponent(btnToggleInfo)
                                .addGap(18, 18, 18)
                                .addComponent(pnChangeProfile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnTogglePw)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(pnChangePw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(139, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSavePwActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSavePwActionPerformed
        doSavePassword();
    }//GEN-LAST:event_btnSavePwActionPerformed

    private void btnToggleInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToggleInfoActionPerformed
        Helper.toggle(pnChangeProfile);
    }//GEN-LAST:event_btnToggleInfoActionPerformed

    private void btnTogglePwActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTogglePwActionPerformed
        Helper.toggle(pnChangePw);
    }//GEN-LAST:event_btnTogglePwActionPerformed

    private void btnSaveInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveInfoActionPerformed
        doSaveInfo();
    }//GEN-LAST:event_btnSaveInfoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSaveInfo;
    private javax.swing.JButton btnSavePw;
    private javax.swing.JButton btnToggleInfo;
    private javax.swing.JButton btnTogglePw;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPasswordField jpfConfirmPw;
    private javax.swing.JPasswordField jpfNewPw;
    private javax.swing.JPasswordField jpfOldPw;
    private javax.swing.JPanel pnChangeProfile;
    private javax.swing.JPanel pnChangePw;
    private javax.swing.JTextField txtMobile;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables

}
